<template>
  <div>
    <div class="tools-div">
      <el-button type="success" size="small" @click="showAddMenuDialog(0)">
        添 加
      </el-button>
    </div>

    <el-table
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="title" label="菜单标题" />
      <el-table-column prop="component" label="路由名称" />
      <el-table-column prop="sortValue" label="排序" />
      <el-table-column prop="status" label="状态" #default="scope">
        {{ scope.row.status == 1 ? '正常' : '停用' }}
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button
          type="success"
          size="small"
          @click="showAddMenuDialog(scope.row.id)"
        >
          添加下级节点
        </el-button>
        <el-button
          type="primary"
          size="small"
          @click="handleShowUpdateDialog(scope.row)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelMenu(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>
    <!-- 新增菜单的对话框 -->
    <div>
      <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
        <el-form label-width="120px">
          <el-form-item label="菜单标题">
            <el-input v-model="menu.title" />
          </el-form-item>
          <el-form-item label="路由名称">
            <el-input v-model="menu.component" />
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="menu.sortValue" />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="menu.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveOrUpdate">
              提交
            </el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import {
  FindNestedMenus,
  GetMenuById,
  DeleteMenu,
  UpdateMenu,
  SaveMenu,
} from '@/api/sysMenu'
import { ElMessage, ElMessageBox } from 'element-plus'
//===更新菜单start
let handleShowUpdateDialog = async row => {
  menu.value = {}
  //查询要回显的数据
  const { data } = await GetMenuById(row.id)
  menu.value = data
  //显示对话框
  dialogTitle.value = '更新菜单'
  dialogVisible.value = true
}
let handleUpdateMenu = async () => {
  await UpdateMenu(menu.value)
  ElMessage.success('更新成功')
  dialogVisible.value = false
  fetchData()
}
//===更新菜单end
//===新增菜单start
let dialogVisible = ref(false)
let dialogTitle = ref('添加菜单')
let menu = ref({})
// 添加按钮事件函数
let showAddMenuDialog = pid => {
  menu.value = {}
  //设置要新增的菜单的parentId=0 表示添加一级菜单
  menu.value.parentId = pid

  //显示对话框
  dialogTitle.value = '添加菜单'
  dialogVisible.value = true
}
//对话框提交按钮事件函数
let handleSaveOrUpdate = () => {
  if (menu.value.id) {
    //更新
    handleUpdateMenu()
  } else {
    //新增
    handleSaveMenu()
  }
}
//新增
let handleSaveMenu = async () => {
  // console.log(menu.value)
  await SaveMenu(menu.value)
  ElMessage.success('新增成功')
  //清空缓存
  menu.value = {}
  //关闭对话框
  dialogVisible.value = false
  //刷新页面
  fetchData()
}

//===新增菜单end

let list = ref([])
//页面初始化加载数据
onMounted(() => {
  fetchData()
})
//查询嵌套菜单集合数据
let fetchData = async () => {
  const { data } = await FindNestedMenus()
  list.value = data
}
//删除按钮事件函数
let handleDelMenu = row => {
  ElMessageBox.confirm(`删除菜单【 ${row.title} 】，是否确认?`, '确认', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      //确认删除
      await DeleteMenu(row.id)
      ElMessage.success('删除成功')
      //刷新页面数据
      fetchData()
    })
    .catch(error => {
      if (error === 'cancel') {
        //取消删除
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      }
    })
}
</script>
<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
